package com.secneo.appshare.threads;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.mime.qweibo.QWeiboSyncApi;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.UserInfo;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;

//获得access token --> 存储数据   --> (获得用户信息   del) --> 像服务器注册
public class QWeiboGetAccessToken extends Thread{
	
	private Context ctx = null ;
	private Handler handler = null ;
	
	private String request_token = new String() ;
	private String request_token_secret = new String() ;
	private String verify = new String() ;
	
	private String access_token = new String() ;
	private String access_token_secret = new String() ;
	
	
	public QWeiboGetAccessToken(Context ctx , Handler handler ,String request_token ,
			String request_token_secret , String verify){
		this.ctx = ctx ;
		this.handler = handler ;
		this.request_token = request_token ;
		this.request_token_secret = request_token_secret ;
		this.verify = verify ;
	}
	
	public void run(){
		QWeiboSyncApi api = new QWeiboSyncApi();
    	String str = api.getAccessToken(Constants.QWEIBO_APP_KEY
    			, Constants.QWEIBO_APP_SECRET, request_token, 
    			request_token_secret, verify);
    	Message msg = new Message() ;
    	Bundle data = new Bundle() ;
    	Log.d("qq", "switch access token str = " + str) ;
    	if(parseToken(str)){
    		Log.d("qq", "get access token token = " + access_token) ;
    		Log.d("qq", "get access token token_secret = " + access_token_secret) ;	
    		LocalStore localStroe = LocalStore.getLocaleStoreInstance(ctx) ;
    		localStroe.setTencentAccountActive() ;
    		localStroe.setQWeiboAccessToken(access_token) ;
    		localStroe.setQWeiboAccessTokenSecret(access_token_secret) ;
    		
    		/*String t1 = localStroe.qWeiboAccessToken() ;
    		String t2 = localStroe.qWeiboAccessTokenSecret() ;*/
			
    		localStroe.getTencentUserInfo() ;
			
    		boolean first_login = (localStroe.getAndroidId() == null ||
    				localStroe.getAndroidId() == "") ? true : false ;
    		
    		RemoteApiImpl apiImpl = new RemoteApiImpl() ;
    		UserInfo userInfo = null;
    		
    		String localName = LocalStore.getLocaleStoreInstance(ctx).getNickName();
			String localIcon = LocalStore.getLocaleStoreInstance(ctx).getAvatarName();
			if(localName.equals(""))
				userInfo = apiImpl.addNewWeiboUser(ctx, 2 , localStroe.qWeiboName() , localStroe.qWeiboNickName(), localStroe.qWeiboImgUrl());
			else
				userInfo = apiImpl.addNewWeiboUser(ctx, 2, localStroe.qWeiboName(), localName, localIcon);
    		
			
			
    		if(userInfo != null)
    		{
    			if(userInfo.id.equals(Constants.SNS_HAS_ASSOCIATE) || 
    					userInfo.id == Constants.SNS_HAS_ASSOCIATE){
    				
					LocalStore.getLocaleStoreInstance().logoutTencentAccount();
					
    				msg.what = 5 ;
    				data.putString("nickName", localStroe.qWeiboNickName()) ;
    				msg.setData(data) ;
    				handler.sendMessage(msg) ;
    				return ;
    			}
    			if(userInfo.id != null || !("".equals(userInfo.id)))
    			{
    				localStroe.setUid(userInfo.id);
    				localStroe.setAndroidId(userInfo.androidId);
    				Log.d("qq" , "addNewUser uid = " + userInfo.id) ;
    			}
    		} 
    		else 
    		{
    			msg.what = 3 ;
    			handler.sendMessage(msg) ;
    			return ;
    		}
    		
			if(localName.equals(""))
			{
			//	localStroe.setNickName(localStroe.qWeiboNickName());
			//	localStroe.setAvatarName(localStroe.qWeiboImgUrl());
				localStroe.setNickName(userInfo.nick);
				localStroe.setAvatarName(localStroe.qWeiboImgUrl());
			}
    		
			//success
    		msg.what = 2 ;
    		data.putBoolean("first_login", first_login) ;
        	msg.setData(data) ;
        	handler.sendMessage(msg) ;
    	} else {
    		msg.what = 3 ;
    		handler.sendMessage(msg) ;
    	}
    	
	}
	
	private boolean parseToken(String response) {
		if (response == null || response.equals("")) {
			return false;
		}

		String[] tokenArray = response.split("&");

		if (tokenArray.length < 2) {
			return false;
		}

		String strTokenKey = tokenArray[0];
		String strTokenSecrect = tokenArray[1];

		String[] token1 = strTokenKey.split("=");
		if (token1.length < 2) {
			return false;
		}
		access_token = token1[1];

		String[] token2 = strTokenSecrect.split("=");
		if (token2.length < 2) {
			return false;
		}
		access_token_secret = token2[1];
		
		return true;
	}
}
